package com.module.ble.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.module.ble.db.table.UserEnvTemperature
import kotlinx.coroutines.flow.Flow

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2024/10/18
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
@Dao
interface EnvTemperatureDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(vararg bean: UserEnvTemperature)

    @Query("SELECT * FROM env_temperature")
    fun findAll(): MutableList<UserEnvTemperature>

    //根据时间范围查询数据
    @Query("SELECT * FROM env_temperature WHERE createdTime >= :startTime AND createdTime <= :endTime ORDER BY createdTime ASC")
    fun findByTimeRange(startTime: Long, endTime: Long): List<UserEnvTemperature>

    // 根据时间范围查询数据（Flow版本）
    @Query("SELECT * FROM env_temperature WHERE createdTime >= :startTime AND createdTime <= :endTime ORDER BY createdTime ASC")
    fun findByTimeRangeFlow(startTime: Long, endTime: Long): Flow<List<UserEnvTemperature>>

    // 查询 所有  倒序 查询
    @Query("SELECT * FROM env_temperature ORDER BY createdTime DESC  LIMIT :mSize")
    fun findByDescSize(mSize: Int): MutableList<UserEnvTemperature>

    @Update
    fun update(msg: UserEnvTemperature)

    @Query("select count(*) from env_temperature")
    fun count(): Int

    //查询某条数据
    @Query("SELECT * FROM env_temperature WHERE createdTime = :time")
    fun findMessageByTime(time:Long): UserEnvTemperature

    //删除所有 → '所有'的只能用@Query,@Query可以编写任意的sql语句
    @Query("DELETE FROM env_temperature")
    fun deleteAll()

    @Query("DELETE FROM env_temperature WHERE createdTime = :time")
    fun deleteMsgByTime(time:Long)
}